<?php
	namespace app\index\controller;

	use \think\Db;

	use \think\Session;

	class Category extends Base{
		public function index(){
			try{

				Session::set('zmy','张明瑶666');

				$type=input('get.type');

				$row=$this->getCache('category');

				if($type=='parent'){
					$row2=[];
					foreach($row as $k=>$v){
						if($v['c_parentId']==0){
							$row2[]=$v;
						}
					}

					$row=$row2;
				}


				return $this->data($row);
			}catch(\Exception $e){
				return $this->err($e);
			}
		}

		public function goods(){
			try{
				$id=input('get.id');
				
				$row=$this->getGoods();

				if($id){

					$row2=[];

					foreach($row as $k=>$v){
						if($k==$id){
							$row2=$v;
						}
					}

					$row=$row2;
				}

				return $this->data($row);

			}catch(\Exception $e){

				return $this->err($e);

			}
		}

		private function getGoods(){

			$row=$this->me->get('cateGood');

			if(empty($row)){
				$category=$this->getCache('category');

				$category=$this->getCateAll($category);


				$row=[];

				foreach($category as $k=>$v){

					// $cids=join($v,',');

					$goods=Db::name('goods')->where(['g_dataFlag'=>1,'g_cateId'=>['in',$v]])->order('g_id desc')->limit(20)->select();

					$row[$k]=$goods;

				}

				$this->me->set('cateGood',$row);
			}
			
			return $row;
		}
	}
?>